package com.five.novel.controller;
import com.five.basic.result.PageList;
import com.five.exception.BusinessException;
import com.five.novel.service.INovelService;
import com.five.pojo.Novel;
import com.five.result.Result;
import com.five.vo.NovelRankVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/novel")
public class NovelController {

    @Autowired
    private INovelService novelService;

    /*
     * 创建小说
     */
    @PostMapping("/add")
    public Result add(@RequestBody Novel novel) {
        try {
            novelService.add(novel);
            return Result.success("添加成功");
        } catch (Exception e) {
            return Result.error("添加失败");
        }
    }

    /*
     * 更新小说
     */
    @PostMapping("/update")
    public Result update(@RequestBody Novel novel) {
        try {
            novelService.update(novel);
            return Result.success("更新成功");
        } catch (Exception e) {
            return Result.error("更新失败");
        }
    }

    /*
     * 删除小说
     */
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        try {
            novelService.delete(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error("删除失败");
        }
    }

    /*
     * 批量删除小说
     */
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        try {
            novelService.deleteBatch(ids);
            return Result.success("批量删除成功");
        } catch (Exception e) {
            return Result.error("批量删除失败");
        }
    }

    /*
     * 分页查询小说
     */
    @PostMapping("/queryPage")
    public Result queryPage(@RequestBody Novel novel) {
        try {
            PageList<Novel> list = novelService.queryPage(novel);
            return Result.success(list);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    /*
     * 查询小说数量
     */
    @PostMapping("/queryCount")
    public Result queryCount(@RequestBody Novel novel) {
        try {
            int count = novelService.queryCount(novel);
            return Result.success(count);
        } catch (Exception e) {
            return Result.error("查询失败");
        }
    }

    /*
     * 查询所有小说
     */
    @PostMapping("/queryAll")
    public Result queryAll() {
        try {
            List<Novel> list = novelService.queryAll();
            return Result.success(list);
        } catch (Exception e) {
            return Result.error("查询失败");
        }
    }

    @PostMapping("/rank/collect/top10")
    public Result<List<NovelRankVo>> top10Collect() {
        return Result.success(novelService.rankTop10Collect());
    }

    @PostMapping("/listNovelByCategory")
    public Result<List<NovelRankVo>> listNovelByCategory(@RequestBody Map<String, Long> params) {
        Long categoryId = params.get("categoryId");
        return Result.success(novelService.listNovelByCategory(categoryId));
    }

}